<?php
namespace App\Http\Controllers\Admin;

use App\Models\Printer;

class PeripheralController extends BaseController
{
    public function __construct(Printer $printer)
    {
        parent::__construct();
        $this->printer = $printer;
    }

    public function getIndex()
    {
        $rows = $this->printer->getPrinterList();
        $ctr = 'peripheral';
        return view('admin.peripheral.printer',compact('rows','ctr'));
    }

    public function getPrinterEdit()
    {
        if(request('id')){
            $info = $this->printer->_getOneById(request('id'));
            if (!$info) {
                return $this->error('非法操作');
            }
            $info = $info->toArray();
            return view('admin.peripheral.printer_add', compact('info'));
        } else {
            return view('admin.peripheral.printer_add');
        }

    }

    public function postPrinterEdit()
    {
        $shopInfo = session('shop_info');

        $res = $this->printer->addOrUpdate($shopInfo['admin_id'], $shopInfo['id']);
        if (!$res) {
            return $this->error($this->printer->getError() ? $this->printer->getError() : '未知错误');
        }
        return $this->success('新增成功', urlAdmin('peripheral', 'getIndex'));
    }

    public function getPrinterAdd()
    {
        $printerInfo = $this->printer->_getOneById(request('id'));
        if(!$printerInfo){
            return $this->error($this->printer->getError() ? : '未知错误');
        }
        $printerSupport = new \App\Support\Printer();
        $str = $printerInfo['serial_number'].'#'.$printerInfo['auth_code'];
        $printerStatus = $printerSupport->queryPrinterStatus($printerInfo['serial_number']);
        if($printerStatus == 0) {
            $res = $printerSupport->addPrinter($str);
        } else {
            $res = true;
        }
        if($res) {
            return $this->success('打印机信息上传成功',urlAdmin('peripheral','getIndex'));
        } else {
            $this->error('打印机信息上传失败');
        }
    }

    public function anyChangePrinterStatus()
    {
        $id = request('id');
        $status = request('status');
        $printer = $this->printer->_getOneById($id);
        if (!$printer) {
            return $this->error('该打印机不存在');
        }
        $where['id'] = ['=', $id];
        switch ($status) {
            case 0  :
                return $this->forbid($this->printer, $where, ['success' => '禁用成功', 'error' => '禁用失败']);
            case 1  :
                return $this->resume($this->printer, $where, ['success' => '启用成功', 'error' => '启用失败']);
            default :
                return $this->error('参数错误');
        }
    }

    public function getPrinterDel()
    {
        $id = request('id');
        $printer = $this->printer->_getOneById($id);
        if (!$printer) {
            return $this->error('该打印机不存在');
        }
        (new \App\Support\Printer())->delPrinter($printer['serial_number']);

        $where['id'] = ['=', $id];
        $res = $this->printer->_del($where);
        if(!$res) {
            return $this->error('删除失败');
        }
        return $this->success('删除成功');
    }
}